/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager;

import br.com.foxline.clinicplus.entity.Operator;
import br.com.foxline.clinicplus.entity.Procedure;
import br.com.foxline.clinicplus.entity.ValueProcedure;
import br.com.foxline.clinicplus.enums.GroupProcedure;
import br.com.foxline.clinicplus.enums.TypeProcedure;
import br.com.foxline.clinicplus.service.ProcedureService;
import br.com.foxline.clinicplus.util.Msg;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerProcedure implements Serializable{
    
    @EJB
    ProcedureService procedureService;
    
    private Procedure procedure;
    
    private Operator operator;
    
    private ValueProcedure valueProcedure;
    
    private List<ValueProcedure> valueProcedures;
    
    @PostConstruct
    public void init(){
        this.procedure = new Procedure();
        this.operator = new Operator();
        this.valueProcedure = new ValueProcedure();
        this.valueProcedures = new ArrayList<ValueProcedure>();
    }
    
    public List<SelectItem> selectTypeProcedure() {
        List<SelectItem> list = new ArrayList<SelectItem>();

        for (TypeProcedure ms : TypeProcedure.values()) {
            list.add(new SelectItem(ms, ms.getName()));
        }
        return list;
    }
    
    public List<SelectItem> selectGroupProcedure() {
        List<SelectItem> list = new ArrayList<SelectItem>();

        for (GroupProcedure ms : GroupProcedure.values()) {
            list.add(new SelectItem(ms, ms.getName()));
        }
        return list;
    }

    public List<Procedure> autocomplete(String query){
        return procedureService.autocomplete(query);
    }
    
    public Procedure getProcedure() {
        return procedure;
    }

    public void setProcedure(Procedure procedure) {
        this.procedure = procedure;
    }

    public ValueProcedure getValueProcedure() {
        return valueProcedure;
    }

    public void setValueProcedure(ValueProcedure valueProcedure) {
        this.valueProcedure = valueProcedure;
    }

    public List<ValueProcedure> getValueProcedures() {
        return valueProcedures;
    }

    public void setValueProcedures(List<ValueProcedure> valueProcedures) {
        this.valueProcedures = valueProcedures;
    }

    public Operator getOperator() {
        return operator;
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }
    
    
    public void addValueProcedure(){
        this.valueProcedure.setOperator(operator);
        this.valueProcedures.add(valueProcedure);
        this.operator = new Operator();
        this.valueProcedure = new ValueProcedure();
    }
    
    public void removeValue(int i){
        this.valueProcedures.remove(i);
    }
    
    public void save(){
        procedure.setActive(true);
        procedure.setValueProcedure(valueProcedures);
        procedureService.save(procedure);
        procedure = new Procedure();
        this.valueProcedures = new ArrayList<ValueProcedure>();
        this.operator = new Operator();
        this.valueProcedure = new ValueProcedure();
        Msg.messagemInfo("Procedimento cadastrado com Sucesso !");
    }
    
}
